package com.ljf.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljf.admin.POJO.SysUserDept;
import com.ljf.admin.mapper.SysUserDeptMapper;
import com.ljf.admin.service.SysUserDeptService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Auther: lijinfeng
 * @Date: 2022/4/21
 * @Description 描述:
 */
@Service
public class SysUserDeptServiceImpl extends ServiceImpl<SysUserDeptMapper, SysUserDept> implements SysUserDeptService {

    @Resource
    private SysUserDeptMapper sysUserDeptMapper;

    @Override
    public int updateBatch(List<SysUserDept> list) {
        return baseMapper.updateBatch(list);
    }
    @Override
    public int updateBatchSelective(List<SysUserDept> list) {
        return baseMapper.updateBatchSelective(list);
    }
    @Override
    public int batchInsert(List<SysUserDept> list) {
        return baseMapper.batchInsert(list);
    }

    @Override
    @Transactional
    public void updateDeptUser(Long deptId, Long[] userIds) {
        List<Long> userIdList = Arrays.asList(userIds);
        List<SysUserDept> list = new ArrayList<>();
        for (Long userId : userIds) {
            list.add(new SysUserDept(userId,deptId));
        }
        QueryWrapper<SysUserDept> qw = new QueryWrapper<>();
        qw.in("user_id",userIdList);
        sysUserDeptMapper.delete(qw);
        sysUserDeptMapper.batchInsert(list);

    }
}
